'use strict';

const Controller = require('egg').Controller;

const moment = require('moment');

class UserController extends Controller {
  /*
   * 用户列表
   **/
  async index() {
    const ctx = this.ctx;
    const keyword = ctx.request.query.content || '';
    const sousuo = ctx.request.query.sousuo;
    const sort = ctx.request.query.sort || '';
    const order_by = ctx.request.query.order_by || 'userid';
    const condition = { delete_time: 0 };
    const act = ctx.request.query.act || '';
    if (keyword) {
      if (sousuo === 'userid') {
        condition.userid = keyword; // condition.id = {[this.app.Sequelize.Op.like]: '%' + keyword + '%'};
      } else if (sousuo === 'mobile') {
        condition.mobile = { [this.app.Sequelize.Op.like]: '%' + keyword + '%' };
      } else {
        condition.username = keyword;
      }
    }
    if (act === 'down') {
      const lists = await ctx.service.user.getList(condition, sort ? [[order_by, sort]] : []);
      const csv = ctx.service.user.downloadUserInfo(lists);
      ctx.set('content-type', 'application/octet-stream');
      ctx.attachment('userinfo.csv');
      ctx.body = csv;
      ctx.status = 200;
      return;
    }
    const page = ctx.request.query.page || 1;
    const res = await ctx.service.user.pageList(condition, page, sort ? [[order_by, sort]] : []);
    const lists = res.rows;
    const count = res.count;
    const html_page = await ctx.service.paginator.paginator(count, page);
    const data_list = {
      lists,
      page: html_page,
      sort,
      order_by,
      keyword,
      sousuo,
    };
    await ctx.render(ctx.view_path(), data_list);
  }

  /*
   * 会员详情
   **/
  async detail() {
    const ctx = this.ctx;
    const id = ctx.request.query.id;
    const info = await ctx.service.user.getUserInfo({ userid: id });
    const data_list = {
      info,
    };
    await ctx.render(ctx.view_path(), data_list);
  }

  /*
   * 添加用户
   **/
  async add() {
    const ctx = this.ctx;
    const method = ctx.request.method;
    if (method === 'POST') {
      const postData = ctx.request.body;
      let body = { status: 0 };
      const res = await ctx.service.user.addUser(postData, body);
      if (res) {
        body = { status: 1, message: '操作成功' };
      } else {
        body.message = '操作失败:' + (body.error || '用户添加失败');
      }
      ctx.body = body;
      ctx.status = 200;
    } else {
      await ctx.render(ctx.view_path());
    }
  }

  /*
    * 用户注册统计
    **/
  async user() {
    const ctx = this.ctx;
    let gap = ctx.request.query.gap || 7;
    const timegap = ctx.request.query.timegap || '';
    if (timegap) {
      const time_arr = timegap.split(' - ');
      if (time_arr.length === 2) {
        gap = time_arr;
      }
    } else {
      gap = parseInt(gap);
    }
    const reg = {};
    const login = {};
    reg.today = await ctx.service.user.getUserCount('today');
    reg.yesterday = await ctx.service.user.getUserCount('yesterday');
    reg.week = await ctx.service.user.getUserCount('week');
    reg.lastweek = await ctx.service.user.getUserCount('lastweek');
    login.today = await ctx.service.user.getUserCount('today', 1);
    login.yesterday = await ctx.service.user.getUserCount('yesterday', 1);
    login.week = await ctx.service.user.getUserCount('week', 1);
    login.lastweek = await ctx.service.user.getUserCount('lastweek', 1);
    const reg_total = await ctx.service.user.getUserByDay(gap);
    const log_total = await ctx.service.user.getUserByDay(gap, 1);
    const { start_time, end_time } = ctx.helper.get_between_time(gap);
    const days = moment(end_time * 1000).diff(moment(start_time * 1000), 'days');
    const show_data = [];
    for (let i = 0; i < days; i++) {
      const key = i ? moment(start_time * 1000).add(i, 'days').format('YYYY-MM-DD') : moment(end_time * 1000).format('YYYY-MM-DD');
      const item = {};
      item.users = log_total.has(key) ? log_total.get(key) : 0;
      item.act = reg_total.has(key) ? reg_total.get(key) : 0;
      item.date = key;
      show_data.push(item);
    }
    const data_list = {
      reg,
      login,
      show_data,
      timegap,
      days,
      gap,
    };
    await ctx.render(ctx.view_path(), data_list);
  }


}

module.exports = UserController;
